<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title></title>
    <meta name="renderer" content="webkit"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
    <meta name="apple-mobile-web-app-status-bar-style" content="black"/>
    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <meta name="format-detection" content="telephone=no"/>
    <link rel="stylesheet" href="/resource/common/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/resource/css/public.css" media="all"/>
</head>
<style>
</style>
<body>
<div class="layui-form" style="margin:auto;margin-top:30px;width: 90%">
    <div class="layui-form-item">
        <label class="layui-form-label">标题</label>
        <div class="layui-input-block">
            <input type="text" name="name" th:value="${news.name}" autocomplete="off" placeholder="标题"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">分类</label>
        <div class="layui-input-block">
            <select class="layui-select" name="categoryName">
                <option></option>
                <option th:each="categorie:${categories}" th:selected="${news.categoryName eq categorie.name}"
                        th:value="${categorie.name}"
                        th:text="${categorie.name}"></option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">图片</label>
            <div class="layui-input-inline">
                <input type="tel" name="img" id="img" th:value="${news.img}" readonly
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button type="button" class="layui-btn layui-btn-primary" id="upload"><i class="layui-icon"></i>上传
            </button>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">内容</label>
        <div class="layui-input-block">
            <textarea class="layui-textarea" name="content" th:utext="${news.content}" lay-verify="content"
                      id="LAY_demo_editor"></textarea>
        </div>
    </div>
    <div class="layui-form-item" style="position: fixed;bottom: 0px;right: 10px">
        <div class="layui-input-block">
            <input hidden name="id" th:value="${news.id}">
            <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="Submit">立即提交</button>
            <button type="" class="layui-btn layui-btn-primary close">取消</button>
        </div>
    </div>
</div>
<script type="text/javascript" src="/resource/common/layui/layui.js"></script>
<script type="text/javascript" src=" /resource/js/user.js"></script>
<script type="text/javascript">


    layui.use(['table', 'layer', 'laydate', 'form', 'upload', 'layedit'], function () {
        var table = layui.table,
            $ = layui.$,
            upload = layui.upload,
            layedit = layui.layedit,
            layer = layui.layer,
            form = layui.form
        //普通图片上传
        upload.render({
            elem: '#upload'
            , url: '/upload.do'
            , done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                $("#img").val(res.obje);
                $("#imgDemo").attr("src", res.obje);
                $("#imgDemo").attr("layer-src", res.obje);
                return layer.msg('上传成功');
            }
            , error: function () {
                return layer.msg('上传失败');
            }
        });
        form.on('submit(Submit)', function (data) {
            data.field.content = layedit.getContent(index);
            data.field.digest = layedit.getText(index).substring(0, 60);
            $.ajax({
                url: '/news/news.do',
                type: 'put',
                data: data.field,
                dataType: 'json',
                async: false,
                success: function (result) {
                    if (!result.status) {
                        parent.layer.msg(result.msg, {time: 2000});
                        return false;
                    } else {
                        layer.msg("修改成功!", {time: 1000});
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                        parent.layui.table.reload('tableId', {});
                    }
                }
            });
        });
        layedit.set({
            uploadImage: {
                url: '/layeditUpload.do'
            }
        });
        var index = layedit.build('LAY_demo_editor');
        $('.close').on('click', function () {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        })
    });

</script>

</body>
</html>